package com.chen.librarysystem.menu;

import com.chen.librarysystem.context.AppContext;
import com.chen.librarysystem.entry.User;
import com.chen.librarysystem.log.LibraryLog;
import com.chen.librarysystem.service.UserService;
import com.chen.librarysystem.service.impl.UserServiceImpl;
import com.chen.librarysystem.util.PrintUtils;

import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;

public class AdminUserManagementMenu implements Menu {
    private static final UserService userService = new UserServiceImpl();
    private static final Scanner sc = AppContext.getInstance().getScanner();

    @Override
    public void start() {
        while (true) {
            System.out.println("=========欢迎来到用户管理界面==========");
            System.out.println("1. 删除用户");
            System.out.println("2. 更新用户信息");
            System.out.println("3. 显示所有用户");
            System.out.println("4. 根据用户名查询用户");
            System.out.println("5. 退出用户管理界面");
            int type = PrintUtils.getIntInput(sc, "请输入您的选择:");
            switch (type) {
                case 1:
                    LibraryLog.log(Level.INFO, "删除用户");
                    deleteUser();
                    break;
                case 2:
                    LibraryLog.log(Level.INFO, "修改用户信息");
                    updateUser();
                    break;
                case 3:
                    LibraryLog.log(Level.INFO, "显示所有用户");
                    getAllUser();
                    break;
                case 4:
                    LibraryLog.log(Level.INFO, "根据用户名查找用户");
                    getUserByName();
                    break;
                case 5:
                    LibraryLog.log(Level.INFO, "用户退出用户管理界面");
                    return;
                default:
                    System.out.println("输入错误，请重新输入！");
            }
        }
    }

    private void getUserByName() {
        String username = PrintUtils.getInput("请输入用户名：");
        User user = userService.getUserByName(username);
        if (user == null) {
            System.out.println("用户不存在！");
            return;
        }
        PrintUtils.printUsers(List.of(user));
        LibraryLog.log(Level.INFO, "查询用户信息成功！");
    }

    private void getAllUser() {
        List<User> users = userService.getAllUsers();
        if (users.isEmpty()) {
            System.out.println("没有用户！");
            LibraryLog.log(Level.INFO, "暂无数据！");
            return;
        }
        PrintUtils.printUsers(users);
        LibraryLog.log(Level.INFO, "查询用户信息成功！");
    }

    private void updateUser() {
        String username = PrintUtils.getInput("请输入要更新的用户名:");
        String password = PrintUtils.getInput("请输入要更新的密码:");
        String role = PrintUtils.getInput("请输入要更新的用户角色:");
        User user = new User(username, password, role);
        if (userService.updateUser(username, user)) {
            LibraryLog.log(Level.INFO, "用户更新成功！");
            System.out.println("用户更新成功！");
            return;
        }
        LibraryLog.log(Level.WARNING, "用户更新失败！");
        System.out.println("用户更新失败！");
    }

    private void deleteUser() {
        String username = PrintUtils.getInput("请输入要删除的用户名:");
        if (userService.deleteUser(username)) {
            System.out.println("删除成功！");
            LibraryLog.log(Level.INFO, "用户:" + username + "删除成功！");
            return;
        }
        System.out.println("删除失败！");
        LibraryLog.log(Level.WARNING, "用户:" + username + "不存在！");
    }
}
